package com.delaphone.flocash.web.action;

import com.delaphone.flocash.model.FEnrolledUsers;
import com.delaphone.flocash.service.PaymentService;
import com.delaphone.flocash.web.util.Validate;

/**
 *
 * @author dongtv
 */
public class EditBillAction extends BaseAction {

    private String id;
    private String billerName;
    private String accNo;
    private String accName;
    private byte billDate;
    private PaymentService payment;
    private String message;

    public String edit_bill() {
        logger.info("Bill ID:" + id);
        FEnrolledUsers r = payment.getEnrolledUser(Integer.parseInt(id));
        billerName = r.getFBillServices().getServiceName();
        accNo = r.getAccountId();
        accName = r.getBillName();
        billDate = r.getBillDate();
        return INPUT;
    }

    public String do_edit_bill() {
        try {
            if (Validate.isEmptyString(accNo)) {
                addActionError(getText("user.edit_bill.invalidAcc"));
            }
            if (Validate.isEmptyString(accName)) {
                addActionError(getText("user.edit_bill.invalidUser"));
            }
            if (!getActionErrors().isEmpty()) {
                return INPUT;
            }
            logger.info("id:" + id);
            logger.info("account No:" + accNo);
            logger.info("account Name:" + accName);
            //update bill information;
            boolean ret = payment.updateEnrolledUser(Integer.parseInt(id), accNo, accName, billDate);
            logger.info("Update bill:" + ret);
            setMessage(getText("user.edit_bill.success"));
            return SUCCESS;
        } catch (Exception ex) {
            logger.error("Error:", ex);
            return ERROR;
        }
    }

    public String delete_bill() {
        logger.info("Bill ID:" + id);
        FEnrolledUsers r = payment.getEnrolledUser(Integer.parseInt(id));
        billerName = r.getFBillServices().getServiceName();
        accNo = r.getAccountId();
        accName = r.getBillName();
        return INPUT;
    }

    public String do_delete_bill() {
        try {
            if (Validate.isEmptyString(accNo)) {
                addActionError(getText("user.edit_bill.invalidAcc"));
            }
            if (Validate.isEmptyString(accName)) {
                addActionError(getText("user.edit_bill.invalidUser"));
            }
            if (!getActionErrors().isEmpty()) {
                return INPUT;
            }
            logger.info("delete bill id:" + id);
            //update bill information;
            boolean ret = payment.deleteEnrolledUser(Integer.parseInt(id));
            logger.info("Update bill:" + ret);
            setMessage(getText("user.delete_bill.success"));
            return SUCCESS;
        } catch (Exception ex) {
            logger.error("Error:", ex);
            return ERROR;
        }
    }

    public String show_bill() {
        return SUCCESS;
    }

    public String getAccName() {
        return accName;
    }

    public void setAccName(String accName) {
        this.accName = accName;
    }

    public String getAccNo() {
        return accNo;
    }

    public void setAccNo(String accNo) {
        this.accNo = accNo;
    }

    public byte getBillDate() {
        return billDate;
    }

    public void setBillDate(byte billDate) {
        this.billDate = billDate;
    }

    public String getBillerName() {
        return billerName;
    }

    public void setBillerName(String billerName) {
        this.billerName = billerName;
    }

    public PaymentService getPayment() {
        return payment;
    }

    public void setPayment(PaymentService payment) {
        this.payment = payment;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}
